package cn.dckeji.dcssm.controller;


import cn.dckeji.dcssm.common.model.ResultModel;
import cn.dckeji.dcssm.entity.Countdown;
import cn.dckeji.dcssm.entity.His;
import cn.dckeji.dcssm.entity.Users;
import cn.dckeji.dcssm.service.CountdownService;
import cn.dckeji.dcssm.service.HisService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Kenny
 * @since 2019-08-01
 */
@RestController
@RequestMapping("/his")
public class HisController {

   @Autowired
   private HisService  hisService;

   @Autowired
   private CountdownService countdownService;

    /**
     * 分页查询管理员
     * @param pageNumber
     * @param pageSize
     * @param
     * @return
     */
    @GetMapping("/page")
    public JSONObject page(Integer pageNumber, Integer pageSize, @RequestParam(value = "userPhone",required = false) String userPhone, @RequestParam(value = "userName",required = false) String userName, @RequestParam(value = "parkNo",required = false) String parkNo) {
        Map<String,Object> maps = new HashMap<>();
        if (StringUtils.isNotBlank(userPhone)) {
            maps.put("userPhone", userPhone);
        }else{
            maps.put("userPhone", null);
        }
        if (StringUtils.isNotBlank(userName)) {
            maps.put("userName", userName);
        }else{
            maps.put("userName", null);
        }
        if (StringUtils.isNotBlank(parkNo)) {
            maps.put("parkNo", parkNo);
        }else{
            maps.put("parkNo", null);
        }
        IPage<Map<String,Object>> page = hisService.getPage(new Page<>(pageNumber, pageSize), maps);
        return ResultModel.page(page.getTotal(),  page.getRecords());
    }


    /**
     * 保存管理员
     * @param
     * @return
     */
    @PostMapping("/setSetting")
    public ResultModel<Object> setSetting(Countdown countdown) {
        String  startTime =   countdown.getTimes().toString()+":00";
        String  endTime =   countdown.getTimes2().toString()+":00";


        //转成LocalDateTime
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


        LocalDateTime startTime1 = LocalDateTime.parse(startTime,df);
        LocalDateTime endTime2 = LocalDateTime.parse(endTime,df);

        LocalDateTime now = LocalDateTime.now();
        Long newSecond = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();//当前时间
        Long newSecond1 = startTime1.toInstant(ZoneOffset.of("+8")).toEpochMilli();//开抢时间
        Long newSecond2 = endTime2.toInstant(ZoneOffset.of("+8")).toEpochMilli();//时间

        if(newSecond1<=newSecond){
            return ResultModel.failure("开抢时间不能小于现在的时间!");
        }

        if(newSecond1>=newSecond2){
            return ResultModel.failure("开抢时间不能大于等于结束的时间!");
        }

            countdown.setId("aaaaaaaaaa");
            countdownService.updateById(countdown);
            return ResultModel.success();
    }





}

